package com.quyunshuo.module.home.util

import java.time.LocalDate
import java.util.Calendar
import java.util.Date
import java.util.Locale
import kotlin.random.Random

fun Int.stepToCalorie(): Double {
    return Random(LocalDate.now().toEpochDay()).nextDouble(0.045, 0.055) * this
}


fun Int.stepToM(): Double {
    return Random(LocalDate.now().toEpochDay()).nextDouble(0.45, 0.55) * this
}


fun Int.stepToMillisecond(): Long {
    return Random(LocalDate.now().toEpochDay()).nextLong(600, 750) * this
}

fun Double.calorieToStr(): String {
    return String.format(Locale.US, "%.2f", this)
}

fun Double.mToStr(): String {
    return if (this >= 1000) {
        String.format(Locale.US, "%.2f km", this / 1000)
    } else {
        String.format(Locale.US, "%.2f m", this)
    }
}

fun Long.millisecondToStr(): String {
    val totalSeconds = this / 1000
    val hours = totalSeconds / 3600
    val minutes = (totalSeconds % 3600) / 60
    val seconds = totalSeconds % 60

    return buildString {
        if (hours > 0) append("${hours}hour ")
        if (minutes > 0 || hours > 0) append("${minutes}min ")
        append("${seconds}s")
    }.trim()
}

object StepUtils {
    /**
     * 获取最大时间范围
     */
    fun getMaxTimeRange(): Pair<Date, Date> {
        // 计算结束时间：今天0点0分0秒（明天的开始时间）
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.set(Calendar.MINUTE, 0)
        calendar.set(Calendar.SECOND, 0)
        calendar.set(Calendar.MILLISECOND, 0)
        calendar.add(Calendar.DAY_OF_MONTH, 1) // 明天0点0分0秒
        val endTime: Date = calendar.time
        // 计算开始时间：结束时间往前推10天
        calendar.add(Calendar.DAY_OF_MONTH, -10)
        val startTime: Date = calendar.time

        return Pair(startTime, endTime)
    }

    /**
     * 获取今天的时间范围
     */
    fun getTodayRange(): Pair<Date, Date> {
        // 计算结束时间：今天0点0分0秒（明天的开始时间）
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.set(Calendar.MINUTE, 0)
        calendar.set(Calendar.SECOND, 0)
        calendar.set(Calendar.MILLISECOND, 0)
        calendar.add(Calendar.DAY_OF_MONTH, 1) // 明天0点0分0秒
        val endTime: Date = calendar.time
        // 计算开始时间：结束时间往前推1天
        calendar.add(Calendar.DAY_OF_MONTH, -1)
        val startTime: Date = calendar.time

        return Pair(startTime, endTime)
    }

}